<template>
  <div class="app-container">
    <eHeader :query="query" />
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;" height="500">
      <el-table-column :show-overflow-tooltip="true" prop="contractCode" label="合同编号" />
      <el-table-column :show-overflow-tooltip="true" prop="contractName" label="合同名称" />
      <el-table-column :show-overflow-tooltip="true" prop="serviceproviderName" label="所属服务商" />
      <el-table-column :show-overflow-tooltip="true" prop="contractItemName" label="合同类型" />
      <el-table-column :show-overflow-tooltip="true" prop="startTime" label="合同开始时间">
        <template slot-scope="scope">
          <span>{{ parseTimeShort(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="endTime" label="合同结束时间">
        <template slot-scope="scope">
          <span>{{ parseTimeShort(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="contractMoney" label="合同金额" />-->
      <el-table-column :show-overflow-tooltip="true" prop="creatTime" label="合同签订时间">
        <template slot-scope="scope">
          <span>{{ parseTimeShort(scope.row.creatTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['ADMIN','TSERVICEPROVIDERCONTRACT_ALL','TSERVICEPROVIDERCONTRACT_EDIT','TSERVICEPROVIDERCONTRACT_DELETE'])" label="操作" width="180px" align="center">
        <template slot-scope="scope">
          <edit v-permission="['ADMIN','TSERVICEPROVIDERCONTRACT_ALL','TSERVICEPROVIDERCONTRACT_EDIT']" :data="scope.row" :sup-this.sync="supThis" :is-view="true" :is-add="false" />
          <edit v-permission="['ADMIN','TSERVICEPROVIDERCONTRACT_ALL','TSERVICEPROVIDERCONTRACT_EDIT']" :data="scope.row" :sup-this.sync="supThis" :is-view="false" :is-add="false" />
          <el-popover
            :ref="scope.row.id"
            v-permission="['ADMIN','TSERVICEPROVIDERCONTRACT_ALL','TSERVICEPROVIDERCONTRACT_DELETE']"
            placement="top"
            width="180"
          >
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini" />
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { del } from '@/api/tServiceproviderContract'
import { del as delEng, queryEngineerMoney } from '@/api/tEngineerMoney'
import { parseTimeShort } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
export default {
  components: { eHeader, edit },
  mixins: [initData],
  data() {
    return {
      delLoading: false,
      supThis: this,
      data: {}
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },

  methods: {
    parseTimeShort,
    checkPermission,
    beforeInit() {
      this.url = 'api/tServiceproviderContract'
      const sort = 'creatTime,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      if (query.contractCode) {
        this.params['contractCode'] = query.contractCode
      }
      if (query.contractName) {
        this.params['contractName'] = query.contractName
      }
      if (query.serviceproviderName) {
        this.params['serviceproviderName'] = query.serviceproviderName
      }
      if (query.contractItemId) {
        this.params['contractItemId'] = query.contractItemId
      }
      return true
    },
    subDelete(scope) {
      this.delLoading = true
      const id = scope.row.id
      const code = scope.row.contractCode
      this.queryEngineer(id, code).then((tableList) => {
        delEng(tableList).then(() => {
          this.transDelete(id);
        })
      })
    },
    transDelete(id) {
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    },
    queryEngineer(id, code) {
      return queryEngineerMoney(id, code);
    }
  }
}
</script>

<style scoped>

</style>
